package com.ruoyi.project.men.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.men.domain.MenCooperateInfoDO;
import com.ruoyi.project.men.domain.MenCooperateTypeDO;
import com.ruoyi.project.men.service.MenCooperateInfoService;
import com.ruoyi.project.wx.domain.MemberCodeDO;
import com.ruoyi.project.wx.service.MemberCodeService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合作咨询
 */
@Controller
@RequestMapping("/api/cooperate")
public class ApiCooperateController {

    @Autowired
    private com.ruoyi.project.men.service.MenCooperateTypeService MenCooperateTypeService;
    @Autowired
    private MenCooperateInfoService menCooperateInfoService;
    @Autowired
    private MemberCodeService memberCodeService;


    /**
     * 获取合作咨询各种类型
      * @param params status 0技术类型1技术服务2行业
     * @return
     */
    @PostMapping( "/getCooperateTypeList")
    @ResponseBody
    public R getCooperateTypeList(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("status") || StringUtils.isBlank(params.get("status").toString())

            ) {
                return R.error("参数错误");
            }
            params.put("delFlag","0");
            params.put("sore","location");
            params.put("order","desc");
            List<MenCooperateTypeDO> list = MenCooperateTypeService.list(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 添加合作咨询
     * @param params
     * @return
     */
    @PostMapping( "/saveCooperate")
    @ResponseBody
    public R saveCooperate(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("skillType") || StringUtils.isBlank(params.get("skillType").toString())
                    ||!params.containsKey("skillService") || StringUtils.isBlank(params.get("skillService").toString())
                    ||!params.containsKey("trade") || StringUtils.isBlank(params.get("trade").toString())
                    ||!params.containsKey("address")
                    ||!params.containsKey("intro")
                    ||!params.containsKey("need") || StringUtils.isBlank(params.get("need").toString())
                    ||!params.containsKey("customerType") || StringUtils.isBlank(params.get("customerType").toString())
                    ||!params.containsKey("companyName")
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("email") || StringUtils.isBlank(params.get("email").toString())
                    ||!params.containsKey("code") || StringUtils.isBlank(params.get("code").toString())

            ) {
                return R.error("参数错误");
            }
            Map<String,Object> map = new HashMap<>();
            map.put("phone",params.get("phone").toString());
            map.put("code",params.get("code").toString());
            map.put("delFlag","0");
            List<MemberCodeDO> list = memberCodeService.list(map);
            if(list.size()<=0){
                return R.error("验证码有误");
            }else{
                MenCooperateInfoDO menCooperateInfoDO = menCooperateInfoService.getByPhone(params.get("phone").toString());
                if(menCooperateInfoDO!=null){
                    return R.error("本手机号已经提交过,无需重复添加");
                }
            }

            MenCooperateInfoDO menCooperateInfoDO = new MenCooperateInfoDO();
            BeanUtils.populate(menCooperateInfoDO,params);
            menCooperateInfoDO.setCaAt(new Date());
            menCooperateInfoDO.setDelFlag(0);
            menCooperateInfoDO.setId(UuidUtil.get32UUID());
            menCooperateInfoService.save(menCooperateInfoDO);
            return R.ok().put("data",menCooperateInfoDO);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
}
